////////////////////////////////////////////////////////////////////////////////
// Filename: texture.fx
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
Texture2D shaderTexture;
Texture2D shaderTexture1;
Texture2D shaderTexture2;
bool	bNeedClip;
float2 waveMapOffset0;

bool bEnableNoise;

///////////////////
// SAMPLE STATES //
///////////////////
SamplerState SampleType
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};


//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float4 clipplane : SV_ClipDistance0;
    float4 projTex : TEXCOORD1;
    float2 offsetTex : TEXCOORD2;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TextureVertexShader(VertexInputType input)
{
    PixelInputType output;
    
    
	// Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

	// Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);
    
	// Store the texture coordinates for the pixel shader.
    output.tex = input.tex;
    output.projTex = output.position;
    output.offsetTex = input.tex + waveMapOffset0;
    
    float4 plane;
    if(bNeedClip)
    {
        plane = float4(0.0f, 1.0f, 0.0f, 0.0f);
    }
    else
    {
        plane = float4(0.0f, 0.0f, 0.0f, 0.0f);
    }

    output.clipplane = dot(mul(input.position, worldMatrix), plane);
	return output;
}


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TexturePixelShader(PixelInputType input) : SV_Target
{
	float4 textureColor;

	// Sample the pixel color from the texture using the sampler at this texture coordinate location.
	if(!bNeedClip && bEnableNoise)
	{	
		input.projTex.xyz /= input.projTex.w;
		input.projTex.x =  0.5f * input.projTex.x + 0.5f;
		input.projTex.y = -0.5f * input.projTex.y + 0.5f;
		float depth = shaderTexture1.Sample(SampleType, input.projTex.xy);
		if(depth < input.projTex.z)
		{
			float4 offsetMap = shaderTexture2.Sample(SampleType, input.offsetTex);
			offsetMap = (offsetMap * 2.0f) - 1.0f;
			float2 fTexOffset = (offsetMap.xy - 0.5f) * 0.005f;
			input.tex += fTexOffset;
		}
		textureColor = shaderTexture.Sample(SampleType, input.tex);
	}
	else
		textureColor = shaderTexture.Sample(SampleType, input.tex);


    return textureColor;
}


////////////////////////////////////////////////////////////////////////////////
// Technique
////////////////////////////////////////////////////////////////////////////////
technique10 TextureTechnique
{
    pass pass0
    {
        SetVertexShader(CompileShader(vs_4_0, TextureVertexShader()));
        SetPixelShader(CompileShader(ps_4_0, TexturePixelShader()));
        SetGeometryShader(NULL);
    }
}